<!-- 文件表单弹窗 -->
<form id="file-form" lay-filter="file-form" class="layui-form model-form">
    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">文件名称</label>
        <div class="layui-input-block">
            <input id="fileName" name="fileName" placeholder="请输入文件名称" type="text" class="layui-input" maxlength="20" lay-verify="required"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文件类型</label>
        <div class="layui-input-block">
            <input id="fileType" name="fileType" placeholder="请输入文件类型" type="text" class="layui-input" maxlength="20"
                   lay-verify="required"/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">选择文件</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn layui-btn-normal" id="test3">文件上传</button>
            <input class="layui-upload-file" type="file" accept="undefined" name="file">
            <div class="layui-upload-list">
                <p id="demoText"></p>
            </div>
        </div>
    </div>

    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary close" type="button">取消</button>
        <button class="layui-btn" lay-filter="file-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'crown', 'config', 'upload'], function () {
        var layer = layui.layer;
        var crown = layui.crown;
        var $ = layui.jquery;
        var upload = layui.upload;
        var config = layui.config;

        var token = config.getToken();

        var fileName =$('#fileName').val();

        var uploadInst = upload.render({
            elem: '#test3'
            ,url: '/file/upload'
            ,accept: 'file' //普通文件
            ,headers:{'Authorization':token}
            ,data:{fileName:function(){
                return $('#fileName').val();
            }}
            ,done: function(res){
                layer.closeAll('loading');

                if(res.result.code == 0){
                    layer.msg('上传成功', {icon: 1});
                }else{
                    layer.msg('上传失败', {icon: 1});
                }
                console.log(res)
                crown.finishPopupCenter();
            }
            ,error:function(res){
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

    });
</script>